package com.engine.game;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.component.game.AIComponent;
import com.component.game.InputComponent;
import com.component.game.NullAIComponent;
import com.component.game.NullInputComponent;
import com.component.game.NullPhysicsComponent;
import com.component.game.PhysicsComponent;
import com.component.game.RenderComponent;
import com.component.game.StandardButtonInputComponent;
import com.level.game.GameObjectInfo;
import com.level.game.StageInfo;
import com.physics.game.PhysicsWorld;
import com.render.game.RenderType;
import com.render.game.Stage;

public class GameFabric {

	public static GameObject createGameObject(GameState state, GameObjectInfo info) throws InstantiationException, IllegalAccessException, ClassNotFoundException{	
		
		 Object input = Class.forName(info.input).newInstance();
		 Object ai = Class.forName(info.ai).newInstance();
		 Object physics = Class.forName(info.physics).newInstance();
		 Object render = Class.forName(info.render).newInstance();
		
	
		return new GameObject(info.name, state, (InputComponent)input, (AIComponent)ai, (PhysicsComponent)physics, (RenderComponent)render, info.isMovable, info.stage, info.rendertype, info.renderIndex);
		
	}
	
	public static GameObjectInfo createStandardButton(String name, String render){
		
		return new GameObjectInfo(name, StandardButtonInputComponent.class.getCanonicalName(), NullAIComponent.class.getCanonicalName(),
				NullPhysicsComponent.class.getCanonicalName(), render, false,
				StageInfo.id, RenderType.gui, -1);
		
	}
	
	public static GameObjectInfo createBackground(String name, String render){
		
		return new GameObjectInfo(name, NullInputComponent.class.getCanonicalName(), NullAIComponent.class.getCanonicalName(),
				NullPhysicsComponent.class.getCanonicalName(), render, false,
				StageInfo.id, RenderType.gui, -1);
		
	}
	
	public static Stage createStage(StageInfo info){
		return new Stage(info.viewportMin, info.viewportMax, info.screenMinInWorld, info.screenMaxInWorld);
	}
	
	public static ContactListener createContactListener(String listener) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		Object l = Class.forName(listener).newInstance();
		
		return (ContactListener) l;
	}
	
	public static PhysicsWorld createPhysicsWorld(String world, String listener) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
		
		ContactListener l = createContactListener(listener);
		
		Class<?> cl = Class.forName(world);
		
		Constructor<?> con = cl.getConstructor(ContactListener.class);
		
		Object obj = con.newInstance(l);
		
		return (PhysicsWorld) obj;
		
	}
	
	public static Sprite createSprite(String name){
		
		return new Sprite(new Texture(name));
		
	}
		
}
